<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    .box1 {
      position: relative;
      width: 400px;
      height: 400px;
      background-color: #ff0;
      left: 50px;
      top: 50px;
    }

    .event {
      width: 100%;
      height: 100%;
      position: absolute;
      left: 0;
      top: 0;
    }

    .box2 {
      position: absolute;
      width: 200px;
      background-color: #f0f;
      height: 200px;

    }
  </style>
</head>

<body>
  <div class="box1">
    <div class="box2"></div>
    <div class="event"></div>
  </div>
</body>

<script>
  let box1 = document.querySelector('.box1')
  let box2 = document.querySelector('.box2')
  let event = document.querySelector('.event')
  // console.log(box1.offsetWidth);
  // console.log(box2.offsetWidth);
  // box1.onmousemove = function (event) {
  //   console.log(event.offsetX);
  // };
  let leftX = 0
  let topY = 0
  event.onmousemove = function (event) {
    leftX = event.offsetX - box2.offsetWidth / 2
    topY = event.offsetY - box2.offsetHeight / 2
    console.log(leftX);
    console.log(topY);
  }

  box2.style.left = (leftX + 'px')
  box2.style.top = (topY + 'px')
</script>

</html>